package com.kyzh.core.activities

import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.atools.ui.ArcButton
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.MyCoupon
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.requests.WealRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemMycouponBinding
import com.kyzh.core.utils.startActivity
import kotlinx.android.synthetic.main.activity_mycoupon.*

class MyCouponActivity : BaseActivity() {

    private val beans = arrayListOf<MyCoupon>()
    private val adapter = Adapter(R.layout.item_mycoupon,beans)
    private var p =1
    private var max =2
    private var type = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mycoupon)
        type = intent.getIntExtra(GlobalKeys.TYPE,0)
        initView()
    }

    private fun initView() {
        if (type==0){
            titleView.setText(getString(R.string.coupon))
            tvHistory.visibility = View.VISIBLE
        }else{
            titleView.setText(getString(R.string.useHistory))
            tvHistory.visibility = View.GONE
        }
        tvHistory.setOnClickListener {
            startActivity<MyCouponActivity>(GlobalKeys.TYPE to 1)
        }
        rvList.adapter = adapter
        adapter.setEmptyView(R.layout.empty)
        refresh()
        adapter.loadMoreModule.isEnableLoadMore=true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage=false
        adapter.loadMoreModule.setOnLoadMoreListener {
            if (max<p){
                adapter.loadMoreModule.loadMoreEnd()
            }else{
                getData()
                adapter.loadMoreModule.loadMoreComplete()
            }
        }
        root.setOnRefreshListener {
            refresh()
            root.isRefreshing = false
        }
    }


    private inner class Adapter(layout:Int,beans:ArrayList<MyCoupon>):BaseQuickAdapter<MyCoupon,BaseDataBindingHolder<ItemMycouponBinding>>(layout,beans), LoadMoreModule {
        override fun convert(holder: BaseDataBindingHolder<ItemMycouponBinding>, item: MyCoupon) {
            holder.dataBinding?.data = item
            val tvStatus = holder.getView<ArcButton>(R.id.tvStatus)
            val v1 = holder.getView<View>(R.id.v1)
            when(item.status){
                getString(R.string.unUse) -> {
                    v1.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(context,R.color.colorPrimary))
                    tvStatus.setBackgroundColor(ContextCompat.getColor(context,R.color.colorPrimary))
                }
                getString(R.string.used)->{
                    v1.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(context,R.color.colorPrimary))
                    tvStatus.setBackgroundColor(Color.parseColor("#CCCCCC"))
                }
                getString(R.string.expired)->{
                    v1.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#E5E5E5"))
                    tvStatus.setBackgroundColor(Color.parseColor("#CCCCCC"))
                }
                else->{
                    v1.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(context,R.color.colorPrimary))
                    tvStatus.setBackgroundColor(ContextCompat.getColor(context,R.color.colorPrimary))
                }
            }
        }

    }



    fun getData(){
        WealRequest.myCoupon(type,p){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
    fun refresh(){
        WealRequest.myCoupon(type,1){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.clear()
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
}